package admin_sh

import (
	"fmt"
	"gitee.com/theegg/go-brick/app/model/secondhand/sh_goods"
	"gitee.com/theegg/go-brick/app/model/system/system_images"
	"gitee.com/theegg/go-brick/app/model/user/users"
	"gitee.com/theegg/go-brick/app/service/app"
	"gitee.com/theegg/go-brick/app/service/help/cstruct"
	"github.com/gin-gonic/gin"
	"strconv"
)

// @title GetShGoodsList
// @description 获取二手商品列表
func GetShGoodsList(c *gin.Context) {
	// 检验参数
	var validaParams sh_goods.QueryParams
	if err := c.ShouldBind(&validaParams); err != nil {
		app.NewResponse(c).JsonI18nReturn(6002101, nil, nil)
		return
	}

	// 查询数据
	data, err := sh_goods.GetDataList(cstruct.StructToMap(validaParams))
	if err != nil {
		fmt.Println(err.Error())
		app.NewResponse(c).JsonI18nReturn(6002102, nil, nil)
		return
	}

	returnData := make(map[string]interface{}, 0)
	returnData["data"] = data

	returnData["cat_options"] = sh_goods.SecondhandGoodsCategory
	returnData["type_options"] = sh_goods.TypeOptions
	returnData["currency_options"] = sh_goods.CurrencyUnitOptions
	returnData["district_options"] = sh_goods.BuildDistrictTree(0)

	app.NewResponse(c).JsonI18nReturn(6002100, nil, returnData)
	return
}

// @title ShGoodsAdd
// @description 添加 二手商品
func ShGoodsAdd(c *gin.Context) {
	idStr := c.PostForm("id")
	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		app.NewResponse(c).JsonReturn(false, err.Error(), nil)
		return
	}

	var modelSg *sh_goods.SecondhandGoods
	var errGet error

	if id > 0 {
		modelSg, errGet = sh_goods.GetItemById(id)
		if errGet != nil {
			app.NewResponse(c).JsonReturn(false, errGet.Error(), nil)
			return
		}
	} else {
		modelSg = new(sh_goods.SecondhandGoods)
	}

	// 校验参数
	var validaParams sh_goods.AddParams
	if err := c.ShouldBind(&validaParams); err != nil {
		app.NewResponse(c).JsonReturn(false, "bind-json-"+err.Error(), nil)
		return
	}

	// 卖家
	user, errUser := users.GetItemById(validaParams.SellerId)
	if errUser != nil {
		app.NewResponse(c).JsonReturn(false, errUser.Error(), nil)
		return
	}

	errAu := modelSg.AddOrUpdate(validaParams, user)
	if errAu != nil {
		app.NewResponse(c).JsonReturn(false, errAu.Error(), nil)
		return
	}

	app.NewResponse(c).JsonReturn(true, "获取数据成功", nil)
	return
}

// @title ShGoodsStatus
// @description 修改 商品 状态
func ShGoodsStatus(c *gin.Context) {
	modelShGoods, err := getTargetShGoodsFromParams(c)
	if err != nil {
		app.NewResponse(c).JsonReturn(false, err.Error(), nil)
		return
	}

	_, errStatus := modelShGoods.ModifyStatus()
	if errStatus != nil {
		app.NewResponse(c).JsonReturn(false, errStatus.Error(), nil)
		return
	}

	app.NewResponse(c).JsonI18nReturn(6001000, nil, nil)
	return

}

// @title PartnerImageUpload
// @description 图片上传 cover / content
func ShGoodsImageUpload(c *gin.Context) {
	imageName, errUpload := system_images.SaveImage("sh", "goods", c)
	if errUpload != nil {
		app.NewResponse(c).JsonReturn(false, errUpload.Error(), nil)
		return
	}

	returnData := make(map[string]interface{})
	returnData["file_url"] = app.GetImageStaticUrl(imageName)
	returnData["file_name"] = imageName

	app.NewResponse(c).JsonReturn(true, "上传图片成功!", returnData)
	return
}

/** =========================== 辅助查询 ============================ **/
// @title getTargetShGoodsFromParams
// @description 内置方法　通过参数id获取目标对象
func getTargetShGoodsFromParams(c *gin.Context) (*sh_goods.SecondhandGoods, error) {
	id := c.PostForm("id")
	if id == "" {
		return nil, app.NewError("对不起, ID不能为空")
	}

	// ID 转换格式
	int64Id, errParse := strconv.ParseInt(id, 10, 64)
	if errParse != nil {
		return nil, app.NewError("对不起, ID格式不正确")
	}

	// 获取对象
	item, errTarget := sh_goods.GetItemById(int64Id)
	if errTarget != nil {
		return nil, app.NewError(errTarget)
	}

	// 目标需要存在
	if item == nil {
		return nil, app.NewError("对不起, 目标不存在")
	}

	return item, nil
}
